package com.jrx.anytxn.param.service.investor.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.common.uitl.StringUtils;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.investor.PrInvestorInstallmentFeeReq;
import com.jrx.anytxn.param.dto.investor.PrInvestorInstallmentFeeRes;
import com.jrx.anytxn.param.entity.PrInvestorInstallmentFee;
import com.jrx.anytxn.param.mapper.ext.ExtPrInvestorInstallmentFeeMapper;
import com.jrx.anytxn.param.service.investor.IPrInvestorInstallmentFeeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 分期手续费表 业务接口实现
 *
 * @author 黄秋平
 * @date 2020-5-20 15:15:07
 */
@Service
@CacheConfig(cacheNames = "INVESTOR_INSTALLMENT")
public class PrInvestorInstallmentFeeServiceImpl implements IPrInvestorInstallmentFeeService {

    private static final Logger logger = LoggerFactory.getLogger(PrInvestorInstallmentFeeServiceImpl.class);

    @Resource
    private ExtPrInvestorInstallmentFeeMapper extPrInvestorInstallmentFeeMapper;

    @Resource
    private ISegmentService segmentService;

    @Override
    public TxnPage<PrInvestorInstallmentFeeRes> findAllPageByTenId(Integer pageNum, Integer pageSize, String tenantId) throws TxnException {
        Page<PrInvestorInstallmentFee> page = PageHelper.startPage(pageNum, pageSize,Constant.ORDER_BY_SORT);
        List<PrInvestorInstallmentFee> installmentFeeTableList = extPrInvestorInstallmentFeeMapper.selectByTenId(tenantId);
        if (installmentFeeTableList.isEmpty()) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }
        List<PrInvestorInstallmentFeeRes> res = BeanMapping.copyList(installmentFeeTableList, PrInvestorInstallmentFeeRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @Override
    public PrInvestorInstallmentFeeRes addInstallmentFeeTable(PrInvestorInstallmentFeeReq prInvestorInstallmentFeeReq) throws TxnException {
        if (prInvestorInstallmentFeeReq.getMaxAmount() != null && prInvestorInstallmentFeeReq.getMinAmount() != null && prInvestorInstallmentFeeReq.getMaxAmount().compareTo(prInvestorInstallmentFeeReq.getMinAmount()) < 0) {
            throw new TxnBizException(TxnRespCode.BIZ_ERROR.getCode(), "输入有误,最大费用值要比最小费用值大");
        }
        PrInvestorInstallmentFee prInvestorInstallmentFee = extPrInvestorInstallmentFeeMapper.selectByInstallmentFeeId(prInvestorInstallmentFeeReq.getInvestorInstallmentFeeId(), prInvestorInstallmentFeeReq.getOrganizationId(),prInvestorInstallmentFeeReq.getTenantId());
        if (prInvestorInstallmentFee != null) {
            logger.error(TxnRespCode.DATA_EXISTS.getMsg());
            throw new TxnException(TxnRespCode.DATA_EXISTS.getMsg());
        }
        PrInvestorInstallmentFee installmentFeeTable = BeanMapping.copy(prInvestorInstallmentFeeReq, PrInvestorInstallmentFee.class);
        try {
            installmentFeeTable.setId(segmentService.getId(Constant.PR_INVESTOR_INSTALLMENT_FEE));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(ErrorMsgConstant.ERROR_ID);
        }
        installmentFeeTable.setRecordVersionNumber(Constant.VERSION_NUMBER);
        extPrInvestorInstallmentFeeMapper.insertSelective(installmentFeeTable);
        return BeanMapping.copy(installmentFeeTable, PrInvestorInstallmentFeeRes.class);
    }

    @CacheEvict(key = "'invertorInstallment_'+#installmentFeeTableReq.tenantId+'_'+#installmentFeeTableReq.investorInstallmentFeeId")
    @Override
    public PrInvestorInstallmentFeeRes modifyInstallmentFeeTable(PrInvestorInstallmentFeeReq installmentFeeTableReq) throws TxnException {
        if (installmentFeeTableReq.getMaxAmount() != null && installmentFeeTableReq.getMinAmount() != null && installmentFeeTableReq.getMaxAmount().compareTo(installmentFeeTableReq.getMinAmount()) < 0) {
            throw new TxnBizException(TxnRespCode.BIZ_ERROR.getCode(), "输入有误,最大费用值要比最小费用值大");
        }
        PrInvestorInstallmentFee prInvestorInstallmentFee = extPrInvestorInstallmentFeeMapper.selectByInstallmentFeeIdAndTenId(installmentFeeTableReq.getInvestorInstallmentFeeId(), installmentFeeTableReq.getTenantId());
        if (prInvestorInstallmentFee == null) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }

        PrInvestorInstallmentFee installmentFeeTable = BeanMapping.copy(installmentFeeTableReq, PrInvestorInstallmentFee.class);
        // 更新
        extPrInvestorInstallmentFeeMapper.updateByTenIdAndFeeId(installmentFeeTable);
        return BeanMapping.copy(installmentFeeTable, PrInvestorInstallmentFeeRes.class);

    }

    @CacheEvict(key = "'invertorInstallment_'+#tenantId+'_'+#investorInstallmentFeeId")
    @Override
    public Boolean removeByTenIdAndFeeId(String tenantId, String investorInstallmentFeeId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(investorInstallmentFeeId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int i = extPrInvestorInstallmentFeeMapper.deleteByTemIdAndFeeId(investorInstallmentFeeId, tenantId);
        return i > 0;
    }

    @Override
    public PrInvestorInstallmentFeeRes findByTenIdAndFeeId(String tenantId, String investorInstallmentFeeId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(investorInstallmentFeeId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrInvestorInstallmentFee installmentTable = extPrInvestorInstallmentFeeMapper.selectByInstallmentFeeIdAndTenId(investorInstallmentFeeId, tenantId);
        return BeanMapping.copy(installmentTable, PrInvestorInstallmentFeeRes.class);
    }

    @Cacheable(key = "'invertorInstallment_'+#tenantId+'_'+#investorInstallmentFeeId")
    @Override
    public PrInvestorInstallmentFee findInvestorInstallmentByFeeId(String investorInstallmentFeeId, String ordId, String tenantId) throws TxnBizException {
        PrInvestorInstallmentFee prInvestorInstallmentFee = extPrInvestorInstallmentFeeMapper.selectByInstallmentFeeId(investorInstallmentFeeId, ordId, tenantId);
        if (prInvestorInstallmentFee == null) {
            throw new TxnBizException(TxnRespCode.NO_DATA.getCode(), ErrorMsgConstant.NO_FEE_DATA);
        }
        return prInvestorInstallmentFee;
    }
}
